home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / machserver / 1.098 / raid / semaphore.c < prev    next >
C/C++ Source or Header  |  1990-11-09  |  2KB  |  96 lines

  1. /* 
  2.  * semaphore.c --
  3.  *
  4.  *    Implements semaphores.
  5.  *
  6.  * Copyright 1990 Regents of the University of California
  7.  * Permission to use, copy, modify, and distribute this
  8.  * software and its documentation for any purpose and without
  9.  * fee is hereby granted, provided that the above copyright
  10.  * notice appear in all copies.  The University of California
  11.  * makes no representations about the suitability of this
  12.  * software for any purpose.  It is provided "as is" without
  13.  * express or implied warranty.
  14.  */
  15.  
  16. #ifndef lint
  17. static char rcsid[] = "$Header: /sprite/src/kernel/raid/RCS/semaphore.c,v 1.1 90/11/09 13:16:01 eklee Exp $ SPRITE (Berkeley)";
  18. #endif /* not lint */
  19.  
  20. #include "semaphore.h"
  21.  
  22.  
  23. /*
  24.  *----------------------------------------------------------------------
  25.  *
  26.  * InitSema
  27.  *
  28.  * Results:
  29.  *    None.
  30.  *
  31.  * Side effects:
  32.  *
  33.  *----------------------------------------------------------------------
  34.  */
  35.  
  36. void
  37. InitSema(semaPtr, name, val)
  38.     Sema    *semaPtr;
  39.     char    *name;
  40.     int        val;
  41. {
  42.     Sync_SemInitDynamic(&semaPtr->mutex, name);
  43.     semaPtr->val = val;
  44. #ifdef TESTING
  45.     Sync_CondInit(&semaPtr->wait);
  46. #endif
  47. }
  48.  
  49.  
  50. /*
  51.  *----------------------------------------------------------------------
  52.  *
  53.  * DownSema
  54.  *
  55.  * Results:
  56.  *    None.
  57.  *
  58.  * Side effects:
  59.  *
  60.  *----------------------------------------------------------------------
  61.  */
  62. void
  63. DownSema(semaPtr)
  64.     Sema    *semaPtr;
  65. {
  66.     MASTER_LOCK(&semaPtr->mutex);
  67.     while (semaPtr->val <= 0) {
  68.     Sync_MasterWait(&semaPtr->wait, &semaPtr->mutex, FALSE);
  69.     }
  70.     semaPtr->val--;
  71.     MASTER_UNLOCK(&semaPtr->mutex);
  72. }
  73.  
  74.  
  75. /*
  76.  *----------------------------------------------------------------------
  77.  *
  78.  * UpSema
  79.  *
  80.  * Results:
  81.  *    None.
  82.  *
  83.  * Side effects:
  84.  *
  85.  *----------------------------------------------------------------------
  86.  */
  87. void
  88. UpSema(semaPtr)
  89.     Sema    *semaPtr;
  90. {
  91.     MASTER_LOCK(&semaPtr->mutex);
  92.     semaPtr->val++;
  93.     Sync_MasterBroadcast(&semaPtr->wait);
  94.     MASTER_UNLOCK(&semaPtr->mutex);
  95. }
  96.